<?php
/**
 * @version : 1.0
 * @since June-2008
 * @package : lib
 * @subpackage struct
 * @deprecated Lớp cơ sở CTree.
 * @name : CTree
 * @todo:  Dùng để thao tác với  cây (thuộc tính cây của đối tượng)
*/
class CTree
{
	/**
	 * Enter description here...
	 *
	 * @var unknown_type
	 */
	
      public $nodes;
      public $children_tag = 'childs';
      function __construct($nodes, $children_tag = 'childs')
      {
	$this->nodes = $nodes;
	$this->children_tag = $children_tag;
      }
      function child_nodes($table, $id, $options = array())
      {
            $items = CStore::$default->select_all($table, array('parent_id'=>$id),$options);
            foreach($items as &$item)
            {
                  $item[$this->children_tag] = $this->child_nodes($table,$item['id'],$options);
            }
            return $items;
      }
      function &parent_id_tree()
      {
            $map = array();
            foreach($this->nodes as &$node)
            {
                  $node[$this->children_tag] = array();
                  $map[$node['id']] = &$node;
            }
            foreach($this->nodes as &$node)
            {
                  if(isset($map[$node['parent_id']]))
                  {
                        $map[$node['parent_id']][$this->children_tag][$node['id']] = $node;
                        $map[$node['id']] = &$map[$node['parent_id']][$this->children_tag][$node['id']];
                        unset($this->nodes[$node['id']]);
                  }
            }
            return $this;
      }
      function parse($f)
      {
            return $this->_parse($f, $this->nodes);
      }
      function _parse($f, &$nodes)
      {
      	if(is_array($nodes))
      	{
		foreach($nodes as &$node)
		{
		      $f($node, $this);
		      $this->_parse($f, $node[$this->children_tag]);
		}
      	}
      }
      function flatten()
      {
            $this->items = array();
            $this->parse(create_function('&$node,&$tree','if(isset($node[\'id\']) and !isset($tree->items[$node[\'id\']]))$tree->items[$node[\'id\']] = $node;else $tree->items[] = $node;'));
            return $this->items;
      }
      function &structure_id_tree($items_name = 'childs')
	{
		require_once 'lib/utils/CStructureID.php';
		//$structure_ids = array(ID_ROOT=>1);
		$show_items = array();
		$min = -1;
		$this->nodes = CList($this->nodes)->order('structure_id')->items;
		$this->items = $this->nodes;
		foreach($this->items as &$item)
		{
			if($min==-1)
			{
				$min = CStructureID($item['structure_id'])->level();
			}
			$structure_ids[number_format($item['structure_id'],0,'','')] = $item['id'];
			
			if(CStructureID($item['structure_id'])->level()<=$min)
			{
				$show_items[$item['id']] = $item+(isset($item['childs'])?array():array($items_name=>array()));
			}
			else
			{
				$st = '';
				$parent = $item['structure_id'];
				$item['parent_id'] = isset($structure_ids[CStructureID($parent)->parent()])?
					$structure_ids[CStructureID($parent)->parent()]:0;
				$i = 0;
				while($i<10 and $parent>=CStructureID::ID_ROOT and ($level=CStructureID($parent = CStructureID($parent)->parent())->level())>=$min and $parent and isset($structure_ids[number_format($parent,0,'','')]))
				{
					$i++;
					if($i>=10)
					{
						echo $level.'.'.$parent.'<br>';
						echo $item['id'].'<br>';
					}
					$st = '['.$structure_ids[number_format($parent,0,'','')].'][\''.$items_name.'\']'.$st;
					
				}
				//echo number_format($parent,0,'','').' '.$st.'<br>';
				if($level<$min or $level==0)
				{
					//echo '$show_items'.$st.'['.$item['id'].']<br>';
					eval('$show_items'.$st.'['.$item['id'].'] = $item+array($items_name=>array());');
				}
			}
		}
		$this->nodes = $show_items;
		
		return $this;
	}
	public function make_ul($id = false)
	{
		return $this->ul_li($this->nodes, $id);
	}
	private function ul_li(&$nodes, $id = false)
	{
		if($nodes)
		{
			$code = '<ul '.($id?'id="'.$id.'"':'').'>';
			foreach($nodes as &$node)
			{
				if(isset($node['name']))
				{
					$code .= '<li ><span class="folder">'.$node['name'].'</span>'.$this->ul_li($node['childs']).'</li>';
				}
			}
			$code .= '</ul>';
			return $code;
		}
	}
}
function CTree($nodes, $children_tag='childs')
{
      return new CTree($nodes,$children_tag);
}
?>